App.require(["yDate"], function (yDate) {
    var app = App.getCurrent();

    app.definePopUp("strdatetimePicker", {
        sureHandler: function (ev) {
            var yDate = this.data.yDate, yTime = this.data.yTime;
            this._computedDate(yDate.year, yDate.month, yDate.day);
            this._computedTime(yTime.hour, yTime.minute);
            if (this.data.target) {
                this.data.target.dispatchEvent({ type: "datetimepickersure", date: this.data.yDate, time: this.data.yTime, target: this, data: this.data });
            }
            this.hidden();
        },
        cancelHandler: function (ev) {
            if (this.data.target) {
                this.data.target.dispatchEvent({ type: "datetimepickercancel", date: this.data.yDate, time: this.data.yTime, target: this, data: this.data });
            }
            this.hidden();
        },
        _computedDate: function (year, month, day) {
            var data = this.data, yDate = data.yDate, minDate = data.minDate, maxDate = data.maxDate;
            yDate.year = year;
            yDate.month = month;
            yDate.day = day;
            if (maxDate && maxDate.compare(year, month, day) === -1) {
                yDate.year = maxDate.year;
                yDate.month = maxDate.month;
                yDate.day = maxDate.day;
            }
            if (minDate && minDate.compare(year, month, day) === 1) {
                yDate.year = minDate.year;
                yDate.month = minDate.month;
                yDate.day = minDate.day;
            }
            this.staticPage.setDate();
        },
        _computedTime: function (hour, minute) {
            var data = this.data, yDate = data.yDate, minDate = data.minDate, maxDate = data.maxDate,
                yTime = data.yTime, minTime = data.minTime, maxTime = data.maxTime;
            var year = yDate.year, month = yDate.month, day = yDate.day;

            yTime.hour = hour;
            yTime.minute = minute;

            if (minDate || maxDate) {
                if (maxDate && maxDate.compare(year, month, day) === 0) {
                    if (yTime.hour > maxTime.hour || (yTime.hour == maxTime.hour && yTime.minute > maxTime.minute)) {
                        yTime.hour = maxTime.hour;
                        yTime.minute = maxTime.minute;
                    }
                }
                if (minDate && minDate.compare(year, month, day) === 0) {
                    if (yTime.hour < minTime.hour || (yTime.hour == minTime.hour && yTime.minute < minTime.minute)) {
                        yTime.hour = minTime.hour;
                        yTime.minute = minTime.minute;
                    }
                }
            }
            this.staticPage.setDate();
        },
        clearHandler: function (ev) {
            var yDate = this.data.yDate, yTime = this.data.yTime;
            this._computedDate(yDate.year, yDate.month, yDate.day);
            this._computedTime(yTime.hour, yTime.minute);
            if (this.data.target) {
                this.data.target.dispatchEvent({ type: "datetimepickerclear", date: this.data.yDate, time: this.data.yTime, target: this, data: this.data });
            }
            this.hidden();
        },
        beforeShow: function () {
            if (document.activeElement) document.activeElement.blur();
            var data = this.data;
            if (!(data.year || data.month || data.day || data.hour || data.minute)) {
                var now = new Date();
                data.year = now.getFullYear();
                data.month = now.getMonth() + 1;
                data.day = now.getDate();
                data.hour = now.getHours();
                data.minute = now.getMinutes();
            }
            data.title = data.title || "选择时间";
            data.yDate = new yDate(data.year, data.month, data.day);
            data.yTime = {
                hour: data.hour, minute: data.minute
            };
            if (data.min) {
                data.minDate = new yDate(data.min.year, data.min.month, data.min.day);
                data.minTime = {
                    hour: data.min.hour, minute: data.min.minute
                };
            }
            if (data.max) {
                data.maxDate = new yDate(data.max.year, data.max.month, data.max.day);
                data.maxTime = {
                    hour: data.max.hour, minute: data.max.minute
                };
            }
            this.attachDiyEvent("sure", this.sureHandler);
            this.attachDiyEvent("cancel", this.cancelHandler);
            this.attachDiyEvent("clear", this.clearHandler);
            return true;
        },
    })
});